using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using WIT.DAQ.Core.Models;

namespace WIT.DAQ.Core.Forms;

/// <summary>
/// 工站管理界面
/// </summary>
internal class StationEditForm : BaseForm
{
	/// <summary>
	///
	/// </summary>
	/// <param name="station"></param>
	internal delegate void SaveStationHandler(Station station);

	/// <summary>
	/// 实例化的目的
	/// </summary>
	private readonly EnumFormPurpose PURPOSE;

	/// <summary>
	/// 当次编辑的站点
	/// </summary>
	private readonly Station STATION;

	private const string TITLE_ADD = "添加站点";

	private const string TITLE_EDIT = "编辑站点";

	/// <summary>
	/// Required designer variable.
	/// </summary>
	private IContainer components = null;

	private Button btnCancel;

	private Button btnConfirm;

	private Label label1;

	private ComboBox cmbDevices;

	private TextBox txtName;

	private Label label4;

	private Label label5;

	private TextBox txtDesc;

	private Label label6;

	private Label label7;

	/// <summary>
	///
	/// </summary>
	internal event SaveStationHandler SaveStation;

	/// <summary>
	///
	/// </summary>
	/// <param name="purpose">目的</param>
	/// <param name="station">编辑时传入站点</param>
	internal StationEditForm(EnumFormPurpose purpose, Station station = null)
	{
		InitializeComponent();
		PURPOSE = purpose;
		STATION = station;
		InitDevices();
		switch (PURPOSE)
		{
		case EnumFormPurpose.Add:
			Text = "添加站点";
			break;
		case EnumFormPurpose.Edit:
			Text = "编辑站点";
			txtName.Text = STATION.Name;
			cmbDevices.SelectedValue = STATION.DefaultDeviceId;
			txtDesc.Text = STATION.Desc;
			break;
		}
	}

	private void StationEditForm_Load(object sender, EventArgs e)
	{
		txtName.Select();
	}

	/// <summary>
	/// 加载设备列表
	/// </summary>
	private void InitDevices()
	{
		List<Device> allDevices = Common.GetAllDevices();
		cmbDevices.DataSource = allDevices;
		cmbDevices.DisplayMember = "ShowName";
		cmbDevices.ValueMember = "Id";
		cmbDevices.SelectedIndex = -1;
	}

	/// <summary>
	/// 确认
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	private void btnConfirm_Click(object sender, EventArgs e)
	{
		if (string.IsNullOrEmpty(txtName.Text.Trim()))
		{
			MessageBox.Show("请输入站点名称", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
			return;
		}
		if (cmbDevices.SelectedItem == null)
		{
			MessageBox.Show("请选择此站点的默认采集设备", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
			return;
		}
		Station station = null;
		switch (PURPOSE)
		{
		case EnumFormPurpose.Add:
			station = new Station
			{
				Id = Guid.NewGuid(),
				Name = txtName.Text.Trim(),
				DefaultDeviceId = new Guid(cmbDevices.SelectedValue.ToString()),
				Desc = txtDesc.Text.Trim()
			};
			break;
		case EnumFormPurpose.Edit:
			station = STATION;
			station.Name = txtName.Text.Trim();
			station.DefaultDeviceId = new Guid(cmbDevices.SelectedValue.ToString());
			station.Desc = txtDesc.Text.Trim();
			break;
		}
		this.SaveStation(station);
		Close();
	}

	/// <summary>
	/// 关闭
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	private void btnCancel_Click(object sender, EventArgs e)
	{
		Close();
	}

	/// <summary>
	/// ESC关闭
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	private void StationEditForm_KeyDown(object sender, KeyEventArgs e)
	{
		if (e.KeyCode == Keys.Escape)
		{
			btnCancel_Click(null, null);
		}
	}

	/// <summary>
	/// Clean up any resources being used.
	/// </summary>
	/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	/// <summary>
	/// Required method for Designer support - do not modify
	/// the contents of this method with the code editor.
	/// </summary>
	private void InitializeComponent()
	{
		this.btnCancel = new System.Windows.Forms.Button();
		this.btnConfirm = new System.Windows.Forms.Button();
		this.label1 = new System.Windows.Forms.Label();
		this.cmbDevices = new System.Windows.Forms.ComboBox();
		this.txtName = new System.Windows.Forms.TextBox();
		this.label4 = new System.Windows.Forms.Label();
		this.label5 = new System.Windows.Forms.Label();
		this.txtDesc = new System.Windows.Forms.TextBox();
		this.label6 = new System.Windows.Forms.Label();
		this.label7 = new System.Windows.Forms.Label();
		base.SuspendLayout();
		this.btnCancel.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right;
		this.btnCancel.Location = new System.Drawing.Point(472, 194);
		this.btnCancel.Name = "btnCancel";
		this.btnCancel.Size = new System.Drawing.Size(75, 23);
		this.btnCancel.TabIndex = 4;
		this.btnCancel.Text = "取消(&C)";
		this.btnCancel.UseVisualStyleBackColor = true;
		this.btnCancel.Click += new System.EventHandler(btnCancel_Click);
		this.btnConfirm.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right;
		this.btnConfirm.Location = new System.Drawing.Point(391, 194);
		this.btnConfirm.Name = "btnConfirm";
		this.btnConfirm.Size = new System.Drawing.Size(75, 23);
		this.btnConfirm.TabIndex = 3;
		this.btnConfirm.Text = "确认(&S)";
		this.btnConfirm.UseVisualStyleBackColor = true;
		this.btnConfirm.Click += new System.EventHandler(btnConfirm_Click);
		this.label1.AutoSize = true;
		this.label1.Location = new System.Drawing.Point(22, 66);
		this.label1.Name = "label1";
		this.label1.Size = new System.Drawing.Size(65, 12);
		this.label1.TabIndex = 2;
		this.label1.Text = "默认设备：";
		this.cmbDevices.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
		this.cmbDevices.FormattingEnabled = true;
		this.cmbDevices.Location = new System.Drawing.Point(93, 63);
		this.cmbDevices.Name = "cmbDevices";
		this.cmbDevices.Size = new System.Drawing.Size(250, 20);
		this.cmbDevices.TabIndex = 1;
		this.txtName.Location = new System.Drawing.Point(93, 36);
		this.txtName.MaxLength = 20;
		this.txtName.Name = "txtName";
		this.txtName.Size = new System.Drawing.Size(250, 21);
		this.txtName.TabIndex = 0;
		this.label4.AutoSize = true;
		this.label4.Location = new System.Drawing.Point(22, 39);
		this.label4.Name = "label4";
		this.label4.Size = new System.Drawing.Size(65, 12);
		this.label4.TabIndex = 2;
		this.label4.Text = "站点名称：";
		this.label5.AutoSize = true;
		this.label5.ForeColor = System.Drawing.Color.Red;
		this.label5.Location = new System.Drawing.Point(349, 39);
		this.label5.Name = "label5";
		this.label5.Size = new System.Drawing.Size(17, 12);
		this.label5.TabIndex = 2;
		this.label5.Text = "* ";
		this.txtDesc.Location = new System.Drawing.Point(93, 89);
		this.txtDesc.Multiline = true;
		this.txtDesc.Name = "txtDesc";
		this.txtDesc.Size = new System.Drawing.Size(373, 87);
		this.txtDesc.TabIndex = 2;
		this.label6.AutoSize = true;
		this.label6.Location = new System.Drawing.Point(22, 92);
		this.label6.Name = "label6";
		this.label6.Size = new System.Drawing.Size(65, 12);
		this.label6.TabIndex = 2;
		this.label6.Text = "描    述：";
		this.label7.AutoSize = true;
		this.label7.ForeColor = System.Drawing.Color.Red;
		this.label7.Location = new System.Drawing.Point(349, 66);
		this.label7.Name = "label7";
		this.label7.Size = new System.Drawing.Size(89, 12);
		this.label7.TabIndex = 2;
		this.label7.Text = "* 默认采集设备";
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 12f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.ClientSize = new System.Drawing.Size(559, 229);
		base.Controls.Add(this.txtDesc);
		base.Controls.Add(this.txtName);
		base.Controls.Add(this.label6);
		base.Controls.Add(this.cmbDevices);
		base.Controls.Add(this.label7);
		base.Controls.Add(this.label5);
		base.Controls.Add(this.label4);
		base.Controls.Add(this.label1);
		base.Controls.Add(this.btnConfirm);
		base.Controls.Add(this.btnCancel);
		base.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
		base.MaximizeBox = false;
		base.Name = "StationEditForm";
		base.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
		this.Text = "StationEditForm";
		base.Load += new System.EventHandler(StationEditForm_Load);
		base.KeyDown += new System.Windows.Forms.KeyEventHandler(StationEditForm_KeyDown);
		base.ResumeLayout(false);
		base.PerformLayout();
	}
}
